package ru.housewife.controllers;

import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.ModelAndView;
import ru.common.Jsons;
import ru.common.hibernate.HibernateActionExecutor;
import ru.housewife.AppConstants;
import ru.housewife.cache.CacheManager;
import ru.housewife.form.UserForm;
import ru.housewife.form.WareForm;
import ru.housewife.form.WareUserPriceForm;
import ru.housewife.service.prices.AddWareUserPrice;
import ru.housewife.service.prices.UpdateWareUserPrice;

import javax.servlet.http.HttpServletRequest;
import java.io.PrintWriter;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Controller to manage prices
 *
 * @author Alexander Troshanin
 */
@Controller
public class PricesController extends AuthorizedController implements AppConstants {

    private final HibernateActionExecutor hiber;

    public PricesController(@Qualifier("houseWifesHibernate") HibernateActionExecutor hiber) {
        this.hiber = hiber;
    }

    @RequestMapping(value = "/get-users-prices-list.html")
    public Object getUsersPrices(@RequestParam(required = false, value = "page") Integer pageObj, HttpServletRequest request) {
        int page = pageObj != null ? pageObj : 0;
        UserForm user = getUserForm(request);

        user.checkUsersWaresPrices();

        List<UserForm.UsersWare> usersWares = user.getUsersWares();
        int origSize = usersWares.size();
        int pageCount = getPageCount(SELECT_RECIPES_VIEW_PAGE_SIZE, origSize);
        page = (page >= pageCount) ? 0 : page;
        int from = SELECT_RECIPES_VIEW_PAGE_SIZE * page;
        int to = from + SELECT_RECIPES_VIEW_PAGE_SIZE;
        to = (to >= origSize) ? origSize : to;

        ModelAndView mv = new ModelAndView("housewifes/prices/usersPricesPage");

        mv.addObject("user", user);
        mv.addObject("usersWares", usersWares.subList(from, to));

        mv.addObject("selectedPage", page);
        mv.addObject("pageCount", pageCount);
        mv.addObject("pageNums", generatePageNumsInfo(page, pageCount));

        return mv;
    }

    @RequestMapping(value = "/set-users-ware-price.html")
    public void setUsersWarePrice(long wareId, double warePrice, HttpServletRequest request, PrintWriter writer) {
        UserForm user = getUserForm(request);

        Map mp = setUsersWarePriceInternal(user, wareId, warePrice);
        Jsons.serialize(mp, writer);
    }

    private Map<String, String> setUsersWarePriceInternal(UserForm user, long wareId, double price) {
        Map<String, String> mp = new HashMap<String, String>();
        if (price <= 0) {
            mp.put("result", "FAIL");
            return mp;
        }
/*
        UserForm.UsersWare userWare = user.getUsersWare(wareId);
        if (userWare == null) {
            mp.put("result", "FAIL");
            return mp;
        }
*/
        WareForm ware = CacheManager.getInstance().getWareForm(wareId);
        if (ware == null) {
            mp.put("result", "FAIL");
            return mp;
        }
        WareUserPriceForm userPrice = user.getWareUserPrice(ware.getId());
        if (userPrice == null) {
            userPrice = hiber.execute(new AddWareUserPrice(user, ware, price));
            if (userPrice != null) {
                user.addWareUserPrice(userPrice);
            }
        } else {
            userPrice.setPrice(price);
            hiber.execute(new UpdateWareUserPrice(userPrice));
        }

        mp.put("result", "OK");
        return mp;
    }

}
